package com.ansion.commonchat.startup.workmanager.bwork;

import android.annotation.SuppressLint;
import android.content.Context;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;

import com.ansion.commonchat.startup.workmanager.bwork.utils.futures.BSettableFuture;
import com.google.common.util.concurrent.ListenableFuture;


/**
 * Created by wanghongjie on 2023/5/11
 * Describe :  定义要在后台执行的工作任务。
 */
public abstract class BWorker extends BListenableWorker {

    BSettableFuture<Result> mFuture;

    @Keep
    @SuppressLint("BanKeepAnnotation")
    public BWorker(@NonNull Context context, @NonNull BWorkerParameters workerParams) {
        super(context, workerParams);
    }

    public abstract @NonNull
    Result doWork();

    @Override
    public final @NonNull
    ListenableFuture<Result> startWork() {
        mFuture = BSettableFuture.create();
        getBackgroundExecutor().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Result result = doWork();
                    mFuture.set(result);
                } catch (Throwable throwable) {
                    mFuture.setException(throwable);
                }

            }
        });
        return mFuture;
    }
}